/*
 * Copyright (c) sl All rights reserved.
 * 本软件源代码版权归sl所有,未经许可不得任意复制与传播.
 */
package com.sl.crawler.web.controller;

import com.sl.crawler.common.utils.DateUtils;
import com.sl.crawler.domain.Xiaoqu;
import com.sl.crawler.domain.common.Page;
import com.sl.crawler.robot.Crawler;
import com.sl.crawler.service.XiaoquService;
import com.sl.crawler.service.base.BaseService;
import com.sl.crawler.web.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;

/**
 * @author sl
 */
@Controller
@RequestMapping(value = "/xiaoqu")
public class XiaoquController extends BaseController {
	private static final Logger LOG = LoggerFactory.getLogger(XiaoquController.class);
	@Autowired
	private XiaoquService xiaoquService;

	/**
	* 获取Service操作类
	*/
	@Override
	public BaseService<Xiaoqu, Long> getService() {
	  return xiaoquService;
	}

	/**
	* 获取视图路径
	*/
	@Override
	public String getModelName() {
	  return "xiaoqu";
	}

	/**
	 * 列表展示
	 * @param xiaoqu 实体对象
	 * @param page 分页对象
	 * @return
	 */
	@RequestMapping(method = {RequestMethod.GET,RequestMethod.POST})
	public String list(Xiaoqu xiaoqu,Page<Xiaoqu> page,Model view) throws Exception{
		return baseList(xiaoqu, page, view);
	}

	/**
	 * 响应新增(修改)页面
	 * @param id 对象编号
	 * @return
	 */
	@RequestMapping(value="/edit/{id}")
	public String edit(@PathVariable Long id,Model view) throws Exception{
		return baseEdit(id, view);
	}

	/**
	 * 通过编号删除对象
	 * @param id 对象编号
	 * @return
	 */
	@RequestMapping(value="/delete/{id}")
	public String delete(@PathVariable Long id,Model view) throws Exception{
		return baseDelete(id, view);
	}

	/**
	 * 通过编号查看对象
	 * @param id 对象编号
	 * @return
	 */
	@RequestMapping(value="/{id}",method=RequestMethod.GET)
	public String view(@PathVariable Long id,Model view) throws Exception{
		return baseView(id, view);
	}

	/**
	 * 保存方法
	 * @param xiaoqu 实体对象
	 * @return
	 */
	@RequestMapping(value="/save",method = {RequestMethod.POST,RequestMethod.GET})
	public String save(Xiaoqu xiaoqu,Model view) throws Exception{
		return baseSave(xiaoqu);
	}


	/**
	* 导出CSV
	* @param xiaoqu
	* @return
	* @throws Exception
	*/
	@RequestMapping(value = "exportCsv")
	public void exportCsv(@ModelAttribute("xiaoqu") Xiaoqu xiaoqu, HttpServletResponse response) throws Exception {
    	baseExportCsv(xiaoqu, new Page<Xiaoqu>(1, 5000), response);
	}

	/**
	* 获取导出csv的表头
	* @return
	*/
	@Override
	public String getExportCsvTitle(){
		String title = "主键,小区名称,小区编号,单价,在售数量,最近成交数,正在出租,户型总数,一级地区,二级地区,建筑类型,修建年代,房源标签,是否有效,备注,修改人,创建时间,修改时间,";
		return title;
	}

	/**
	* 获取导出csv的数据明细
	* @param o
	* @return
	*/
	@Override
	public String getExportCsvRow(Object o) {
		Xiaoqu xiaoqu = (Xiaoqu)o;
		StringBuilder sb = new StringBuilder();
    	sb.append("\"").append(xiaoqu.getId()).append("\"").append(",");
    	sb.append(xiaoqu.getXiaoqu()).append(",");
    	sb.append(xiaoqu.getXiaoquId()).append(",");
    	sb.append("\"").append(xiaoqu.getUnitPrice()).append("\"").append(",");
    	sb.append("\"").append(xiaoqu.getSellCount()).append("\"").append(",");
    	sb.append("\"").append(xiaoqu.getDealCount()).append("\"").append(",");
    	sb.append("\"").append(xiaoqu.getLeaseCount()).append("\"").append(",");
    	sb.append("\"").append(xiaoqu.getHouseTypeCount()).append("\"").append(",");
    	sb.append(xiaoqu.getLocationOne()).append(",");
    	sb.append(xiaoqu.getLocationTwo()).append(",");
    	sb.append(xiaoqu.getBuildingType()).append(",");
    	sb.append("\"").append(xiaoqu.getBuildingYears()).append("\"").append(",");
    	sb.append(xiaoqu.getHouseTag()).append(",");
    	sb.append("\"").append(xiaoqu.getYn()).append("\"").append(",");
    	sb.append(xiaoqu.getRemark()).append(",");
    	sb.append(xiaoqu.getModifiedUser()).append(",");
    	sb.append(DateUtils.format(xiaoqu.getCreatedDate())).append(",");
    	sb.append(DateUtils.format(xiaoqu.getModifiedDate())).append(",");
		sb.append("\r\n");
		return sb.toString();
	}

	//自定义扩展

	/**
	 * 导出CSV
	 *
	 * @param house
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/lianJiaXiaoquCrawler", method = {RequestMethod.POST, RequestMethod.GET})
	public String lianJiaXiaoquCrawler(Xiaoqu house, Model view) throws Exception {
		Crawler.crawlerSingle("crawler/lianjia/xiaoqu.yaml");
		return "redirect:/" + getModelName();
	}
}